package com.cx.base.day24_RabbitMQ;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @Author: songyahui
 * @Date: 2020/8/30 18:59
 * @Version 1.0
 */
public class Recv5_Topic {
    private final static String QUEUE_NAME = "topic_exchange_queue_2";
    private final static String EXCHANGE_NAME = "topic_exchange_test";

    public static void main(String[] args) {
        try {
//        获取连接
            Connection connection = ConnectionUtil.getConnection();
//           获取通道
            Channel channel = connection.createChannel();
//            声明队列
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//            绑定队列到交换机，同时指定需要订阅的routing key,订阅 insert update delete
            channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "item.*");
//            定义队列的消费者
            DefaultConsumer consumer = new DefaultConsumer(channel) {
                //             获取消息，并且处理，这个方法类似于事件监听，如果有消息的时候，会被自动调用
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    //                    body 即消息体
                    String msg = new String(body);
                    System.out.println("[消费者1] received :" + msg + "!");
                }
            };
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
